(:
  A procedure to number all items in a Display file
  It suppresses any previous numbering
:)
declare boundary-space strip;

import module namespace
    jj = "http://kumquat.emse.fr/utilitaires"
    at "jjutils.xq" ;
let $t := $arguments[1]
let $vertical := $arguments[2]
let $numberfacet := $arguments[3]
let $items := doc($t)/display/item
let $verticalnodes := for $info in $items/info[@name=$vertical] return jj:get-text($info)
let $verticals := fn:distinct-values($verticalnodes)
return
<display>{
for $vv in $verticals
let $selecteditems := $items[jj:get-text(./info[@name=$vertical])=$vv]
let $turns := fn:count($selecteditems)
let $values := for $item in $selecteditems return xs:integer(jj:get-info($item,$numberfacet))
let $total := fn:sum($values)
let $average := fn:avg($values)
return
  <item>{
    <info name="value">{$vv}</info>,
    <info name="turns">{$turns}</info>,
    <info name="total">{$total}</info>,
    <info name="average">{$average}</info>
  }</item>,
let $turns := fn:count($items)
let $values := for $item in $items return xs:integer(jj:get-info($item,$numberfacet))
let $total := fn:sum($values)
let $average := fn:avg($values)
return
<item>{
    <info name="value">All</info>,
    <info name="turns">{$turns}</info>,
    <info name="total">{$total}</info>,
    <info name="average">{$average}</info>
}</item>  
}</display>
